<template>
  <div class="bottom-bar">
    <div class="check-content">
      <check-button class="check-button" :is-checked="isShow" @click.native="checkClick"/>
      <span>全选</span>
    </div>
    <div class="price">
      合计:￥{{totalPrice}}
    </div>
    <div class="calculate" @click="calcClick">
      去计算({{checkLength}})
    </div>
  </div>
</template>

<script>
  import CheckButton from 'components/content/checkButton/CheckButton'

  export default {
    name: "CartBottomBar",
    data() {
      return {
        cartList: this.$store.state.cartList
      }
    },
    components: {
      CheckButton
    },
    computed: {
      totalPrice() {
        return this.cartList.filter(item => {
          return item.checked
        }).reduce((preValue, item) => {
          return preValue + item.price * item.count
        }, 0).toFixed(2)
      },
      checkLength() {
        return this.cartList.filter(item => item.checked).length
      },
      isShow() {
        if(this.cartList.length === 0)
          return false;
        //高阶函数
        return !this.cartList.find(item => !item.checked);

        //遍历的方法
        // for(let item of this.cartList) {
        //   if(!item.checked)
        //     return false;
        // }
        // return true;
      },
      isMoreOneSelect() {  //至少一个选中
        if(this.cartList.length === 0)
          return false;

        for(let item of this.cartList) {
          if(item.checked) {
            return true;
          }
        }
        return false;
      }
    },
    methods: {
      checkClick() {
        if(this.isShow) { //全部选中
          this.cartList.forEach(item => item.checked = false)
        }
        else {  //有部分不选中
          this.cartList.forEach(item => item.checked = true)
        }
      },
      calcClick() {
        if(!this.isMoreOneSelect) {  //全部未选中
          this.$toast.show('请选择购买的商品');
        }
      }
    }
  }
</script>

<style scoped>
  .bottom-bar {
    position: relative;
    display: flex;
    background-color: #eee;
    height: 40px;
    line-height: 40px;
    font-size: 14px;
  }
  .check-content {
    display: flex;
    align-items: center;
    margin-left: 10px;
    width: 60px;
  }
  .check-button {
    width: 20px;
    height: 20px;
    line-height: 20px;
    margin-right: 5px;
  }
  .price {
    margin-left: 20px;
    flex: 1;
  }
  .calculate {
    width: 100px;
    background-color: red;
    color: #fff;
    text-align: center;
  }
</style>
